#ifndef GAME_CLIENT_IMPL_H
#define GAME_CLIENT_IMPL_H
#include <string>
#include "client_connection.h"
#include "game_base_msg.h"
class Service;
class GameClientPlayer;
class ClientConnection;
class JMsgProto;
class GameClient;
struct GameClientRoomStatus;
class GameClientImpl : public ClientConnectionHandler
{
public:
	GameClientImpl(const std::string& host, int port);
	~GameClientImpl(void);
	void login(const std::string& userName, const std::string& password, bool isGuest);
	void signUp(const std::string& userName, const std::string& password);
	void activate(const string& newPlayerName, const string& newPassword, const std::string& phoneNo);
	void joinRoom(int roomId);
	void sendRoomData(void* data, int len);
	void sendDeskData(void* data, int len);
	void joinDesk(int deskId, int chairId);
	void leaveDesk();
	void setGameClient(GameClient* gameClient);
	void createFriendDesk();
	void joinFriendDesk(const std::string deskCode);
	int getRoomId() { return m_roomId; }
	int getDeskId() { return m_deskId; }
	int getChairId() { return m_chairId; }
	void leaveRoom();
	void updatePlayerInfo();
	void updateRoomStatus();
	GameClientPlayer* getPlayer();
	GameClientRoomStatus* getRoomStatus();
	bool getDeskPlayers(std::vector<GameClientPlayer*>& players);

public:
	virtual void onClientConnected(ClientConnection* conn);
	virtual void onClientData(ClientConnection* conn, int mainCmd, int subCmd, void* data, int len);
	virtual bool onClientDisconnected(ClientConnection* conn);
	
protected:
	void sendClientData(int mainCmd, int subCmd, void* data, int len);
private:
	void procRoomSystemMsg(void* data, int len);
	void procRoomUserMsg(void* data, int len);
	void procDeskSystemMsg(void* data, int len);
	void procDeskUserMsg(void* data, int len);
	void procLoginResult(BaseMsgS2CLogin* msg);
	void procRegisterResult(BaseMsgS2CRegister* msg);
	void procActivateResult(BaseMsgS2CActivate* msg);
	void procJoinRoomResult(BaseMsgS2CJoinRoom* msg);
	void procJoinDeskResult(BaseMsgS2CJoinDesk* msg);
	void procOtherPlayerJoinDesk(BaseMsgDeskS2COtherPlayerJoinDesk* msg);
	void procOtherPlayerLeaveDesk(BaseMsgDeskS2COtherPlayerLeaveDesk* msg);
	void procCreateFriendResult(BaseMsgS2CCreateFriendDesk* msg);
	void procRoomStatus(BaseMsgS2CUpdateRoomStatus* msg);
	
private:
	GameClientPlayer* createPlayerFromMsg(BaseMsgPlayerInfo& playerInfo);

private:
	ClientConnection* m_clientConn;
	Service* m_service;
	JMsgProto* m_proto;
	GameClientRoomStatus* m_roomStatus;
	GameClient* m_gameClient;
	GameClientPlayer* m_player;
	int m_roomId;
	int m_deskId;
	int m_chairId;
	GameClientPlayer* m_deskPlayer[4];
};
#endif

